package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.farmmedcine;
import cn.com.hfuu.module.farmtrim;
import cn.com.hfuu.module.tree;
import cn.com.hfuu.module.user;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.LandService;
import cn.com.hfuu.service.TrimService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.vo.Page;

@Controller
@RequestMapping("trim")
public class TrimControl {
	
	@Autowired
	LandService landser;
	@Autowired
	TrimService trimservice;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	CodeService codeservice;
	
	private String ids;

	List<code> codelist;
	
	@RequestMapping("info")
	public String info(String ids ,Model m){
		codelist=codeservice.selectbyType("04");
		m.addAttribute("codelist", codelist);
		this.ids=ids;
		return "land/farm/trim/trim";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page List(Integer start, Integer limit,int state){
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		List<farmtrim> trimlist=trimservice.selectPage(start,limit,state,ids);
		for (farmtrim trim : trimlist) {
			for (code c : codelist) {
				if(trim.getFarmType()==c.getCodeValue())
					trim.setCode(c);
			}
			trim.setUser(userService.selectuserbyid(trim.getFarmUserid()));
			trim.setLand(landser.selectbyid(trim.getFarmLandid()));
		}
		page.setRoot(trimlist);
		page.setTotal(trimservice.selectPagetotal(state, ids).size());
 		return page;
	}
	
	@RequestMapping("add")
	public ModelAndView add(){
		ModelAndView mv=new ModelAndView();
		mv.addObject("codelist", codelist);
		mv.setViewName("land/farm/trim/add");
		return mv;
		
	}
	
	@RequestMapping("addproc")
	@ResponseBody
	public String addproc(farmtrim f,HttpServletRequest req){
		HttpSession s=req.getSession();
		user u=(user)s.getAttribute("user");
		UUIdUtil idu=new UUIdUtil();
		f.setFarmTrimid(idu.getUuid());
		f.setFarmUserid(u.getUserId());
		SystemUtil sys=new SystemUtil();
		f.setFarmTime(sys.getDatetime());
		f.setFarmLandid(ids);
		if(trimservice.insert(f)>0){
			return "success";
		}
		return "false";
	}
	@RequestMapping("addprocad")
	@ResponseBody
	public String addprocad(@RequestBody farmtrim f,HttpServletRequest req){
		HttpSession s=req.getSession();
		user u=(user)s.getAttribute("user");
		UUIdUtil idu=new UUIdUtil();
		f.setFarmTrimid(idu.getUuid());
		f.setFarmUserid(u.getUserId());
		SystemUtil sys=new SystemUtil();
		f.setFarmTime(sys.getDatetime());
		f.setFarmLandid(ids);
		if(trimservice.insert(f)>0){
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("edit")
	public ModelAndView edit(String id){
		ModelAndView mv=new ModelAndView();
		farmtrim fm=trimservice.selectbyid(id);
		mv.addObject("trim", fm);
		mv.addObject("codelist", codelist);
		mv.setViewName("land/farm/trim/edit");
		return mv;
	}
	
	@RequestMapping("editproc")
	@ResponseBody
	public String editproc(farmtrim fm){
		if(trimservice.upadate(fm)>0){
			return "success";
		}
		return "false";
		
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String[] ids){
		if(trimservice.delete(ids)>0){
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
		 
		codelist=codeservice.selectbyType("04");
 		java.util.List<farmtrim> list = trimservice.selectPagetotal(-1, ids);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (farmtrim m : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			for (code c : codelist) {
				if(m.getFarmType()==c.getCodeValue())
					m.setCode(c);
			}
			m.setUser(userService.selectuserbyid(m.getFarmUserid()));
			m.setLand(landser.selectbyid(m.getFarmLandid()));
			map1.put("编号", m.getFarmTrimid());
			map1.put("活动种类",m.getCode().getCodeName() );
			map1.put("所属茶园", m.getLand().getLandName());
			map1.put("操作人", m.getUser().getUserName());
			map1.put("操作时间", m.getFarmTime());
			mapList.add(map1);
		}
		String[] title = { "编号", "活动种类", "所属茶园", "操作人","操作时间" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶叶杀青信息");
		return new ModelAndView(ve, map);
	}
	
}
